﻿using NLog;
using StackExchange.Redis;
using System.Data;
using TeachlyQBank.DbPool;
using TeachlyQBank.Models;

namespace TeachlyQBank.DbServer
{
    public class RoleServer
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly MySqlConnectionAdapter _mysql; // 操作mysql的类
        private readonly IConnectionMultiplexer _redis;  // 操作redis的类
        private readonly Logger _logger;  //记录日志的类

        // 完成依赖注入
        public RoleServer(MySqlConnectionAdapter mysql, IConnectionMultiplexer redis, IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
            _redis = redis;
            _logger = LogManager.GetCurrentClassLogger();
            _mysql = mysql;
        }

        //功能: 查询所有的用户角色信息
        //返回:查询成功返回一个List<UserRole>包含目标信息，失败返回null
        public List<UserRole> SelAllRoleAndUser()
        {
            List<UserRole> resultList = new List<UserRole>();

            _mysql.Open();

            string query = "SELECT * FROM T_USER LEFT JOIN T_ROLE ON T_USER.role_id = T_ROLE.roleid";

            DataSet data = _mysql.ExecuteQuery(query);

            if (data == null || data.Tables[0].Rows.Count == 0)
            {
                _mysql.Close();
                // 查询出先错误
                _logger.Error($"查询出错,错误的sql语句:{_mysql.GetLastExecutedSql()},错误的信息:{_mysql.GetLastError()}");
                return null;
            }
            else
            {
                foreach (DataRow row in data.Tables[0].Rows)
                {
                    UserRole node= new UserRole();

                    node.role_id = row["role_id"].ToString();
                    node.user_account = row["user_account"].ToString();
                    node.user_name = row["user_name"].ToString();
                    node.user_Id = row["user_id"].ToString();
                    node.role_name = row["rolename"].ToString();

                    resultList.Add(node);
                }
                _mysql.Close();

                return resultList;
            }
        }

        //功能: 根据传递过来的UserRole修改用户的角色
        //参数:一个UserRole里面包含了要修改的类容
        //返回:数据库影响的条数成功返回1，失败返回-1
        public int UpdateUserRole(UserRole userRole)
        {
            _mysql.Open();
            string query = "UPDATE T_USER SET role_id=@role_id where user_id=@user_id;";
            Dictionary<string, object> parameters = new Dictionary<string, object>
            {
                {"@user_id",userRole.user_Id !},
                { "@role_id",userRole.role_id !},
            };
            int count = _mysql.ExecuteNonQuery(query, parameters);
            if (count > 0)
            {
                _logger.Info($"修改了一个用户:{userRole.user_name}的角色,用户ID:{userRole.user_Id},本次花费了{_mysql.GetLastExecutionTime()}毫秒");
                _mysql.Close();
                return count;
            }
            else
            {
                _logger.Error($"修改用户角色出现错误,错误的sql语句:{_mysql.GetLastExecutedSql()},错误的信息:{_mysql.GetLastError()}");
                _mysql.Close();
                return -1;
            }
        }

    }
}
